<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>组织考试</title>
    <link rel="stylesheet" href="/layuimoduls/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuimoduls/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

<!--        添加试卷-->
        <!--    设置科目 设置考试（试卷）名称  选择题库 设置时间-->
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>添加考试</legend>
        </fieldset>

        <form class="layui-form" >

            <div class="layui-form-item">

                <label class="layui-form-label">考试名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="testname" placeholder="请输入试卷名称" class="layui-input">
                </div>

                <label class="layui-form-label">设置科目</label>
                <div class="layui-input-inline">
                    <select name="subjectname" lay-filter="subjectname" id="subjectname">
                        <option value="">请选择一个科目</option>
                        <option th:each="subjectname:${subjects}" th:value="${subjectname}" th:lay-value="${subjectname}"
                                th:text="${subjectname}"></option>
                    </select>
                </div>

                <label class="layui-form-label">考试时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="time" class="layui-input" id="time" placeholder="yyyy-MM-dd HH:mm:ss">
                </div>

            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择试题</label>
                <div class="layui-input-inline">
                    <input type="text" name="questions" placeholder="请选择试题" autocomplete="off" class="layui-input" id="demo">
                </div>

                <label class="layui-form-label">组织试卷</label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">立即添加</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
        <div class="layui-form-item">
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        </div>
<!--        添加试卷结束-->

<!--        自动组卷-->
        <!--    设置科目 设置考试（试卷）名称  选择题库 设置时间-->
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>自动组卷</legend>
        </fieldset>

        <form class="layui-form" >

            <div class="layui-form-item">

                <label class="layui-form-label">考试名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="testname2" placeholder="请输入试卷名称" class="layui-input">
                </div>

                <label class="layui-form-label">设置科目</label>
                <div class="layui-input-inline">
                    <select name="subjectname2" lay-filter="subjectname" id="subjectname2">
                        <option value="">请选择一个科目</option>
                        <option th:each="subjectname:${subjects}" th:value="${subjectname}" th:lay-value="${subjectname}"
                                th:text="${subjectname}"></option>
                    </select>
                </div>

                <label class="layui-form-label">考试时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="time2" class="layui-input" id="time2" placeholder="yyyy-MM-dd HH:mm:ss">
                </div>

            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择试题</label>
                <div class="layui-input-inline">
                    <input type="text" name="nums" placeholder="请输入试题数量" class="layui-input">
                </div>

                <label class="layui-form-label">组织试卷</label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="demo2">自动组卷</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
        <div class="layui-form-item">
            <table class="layui-hide" id="currentTableId2" lay-filter="currentTableFilter"></table>
        </div>
    </div>
</div>
</body>
<script src="/layuimoduls/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/layuimoduls/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/jquery/jquery-3.6.0.js"></script>
<script th:inline="none">
    var  subjectname
    layui.use(['table','form','tableSelect'], function () {
        var  form = layui.form,
            laydate = layui.laydate,
            tableSelect = layui.tableSelect,
            table = layui.table;
        //考试日期范围
        laydate.render({
            elem: '#time',
            type: 'datetime',
            range: 'true'
        });
        laydate.render({
            elem: '#time2',
            type: 'datetime',
            range: 'true'
        });

        form.on('select(subjectname)', function(data){

            subjectname = $("#subjectname").val(data.value)
            console.log(subjectname.val()); //得到美化后的DOM对象
            tableSelect.render({
                elem: '#demo',
                checkedKey: 'questionid',
                height:'400',
                width:'900',
                searchType: 'more',
                searchList: [
                    {searchKey: 'stem', searchPlaceholder: '搜索试题内容'},
                    {searchKey: 'id', searchPlaceholder: '搜索试题编号'},
                ],
                table: {
                    url: '/queryexamquestions',
                    method:'post',
                    contentType: 'application/json;charset=utf-8',
                    where:{"subjectname":subjectname.val()},
                    //$('#subjectname').find('option:selected').val()
                    cols: [[
                        { type: 'checkbox' },
                        { field: 'questionid', title: 'ID' },
                        { field: 'stem', title: '题干' },
                        { field: 'score', title: '分数' }
                    ]],
                    multiple:true,
                    page: true,
                    //分页
                    parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                        var result;
                        console.log(this);
                        console.log(JSON.stringify(res));
                        if (this.page.curr) {
                            result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                        } else {
                            result = res.data.slice(0, this.limit);
                        }
                        return {
                            "code": res.code, //解析接口状态
                            "msg": res.msg, //解析提示文本
                            "count": res.count, //解析数据长度
                            "data": result //解析数据列表
                        }
                    }
                },
                done: function (elem, data) {
                    var NEWJSON = []
                    layui.each(data.data, function (index, item) {
                        NEWJSON.push(item.questionid)
                    })
                    questionids = NEWJSON
                    elem.val(NEWJSON.join(","))
                    table.render({
                        elem: '#currentTableId',
                        url: '/getquestionbyid',
                        method:'post',
                        contentType : "application/json;charsetset=UTF-8",//必须
                        where: {"questionids":NEWJSON},
                        cols: [[
                            {field: 'questionid', title: 'ID', sort: true},
                            {field: 'stem', title: '题干',},
                            {field: 'opta', title: '选项A'},
                            {field: 'optb', title: '选项B'},
                            {field: 'optc', title: '选项C'},
                            {field: 'optd', title: '选项D'},
                            {field: 'right', title: '答案'},
                            {field: 'analyse', title: '解析'},
                            {field: 'type', title: '类型'},
                            {field: 'subjectname', title: '科目'},
                            {field: 'score', title: '分值', sort: true},
                        ]],
                        limits: [5, 10, 10, 20],
                        limit: 10,
                        page: true,
                        skin: 'line',
                        //分页
                        parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数var result;
                            // console.log(this);
                            // console.log(JSON.stringify(res));
                            if (this.page.curr) {
                                result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                            } else {
                                result = res.data.slice(0, this.limit);
                            }
                            return {
                                "code": res.code, //解析接口状态
                                "msg": res.msg, //解析提示文本
                                "count": res.count, //解析数据长度
                                "data": result //解析数据列表
                            }
                        }
                    })
                }
            });

        });


        //监听提交
        form.on('submit(demo1)', function (data) {
            var  questions= $("#demo").val();
            data.questionids = {"questionids":(questions.split(','))}
            var data = JSON.stringify(data.field)
            layer.alert(data)
            $.post({
                url: "/addExamPage",
                data: data,
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                dataType: 'json',
                success: function (response) {
                    if (response.success) {
                        layer.msg(response.msg)
                        //parent.location.reload(); //刷新父页面
                    } else {
                        layer.msg(response.msg)
                    }
                }
            })
        });


        form.on('submit(demo2)', function (data) {
            var data = JSON.stringify(data.field)
            // layer.msg(data)
            $.post({
                url: "/addExamPage2",
                data: data,
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                dataType: 'json',
                success: function (response) {
                    if (response.success) {
                        layer.msg(response.msg)
                        //parent.location.reload(); //刷新父页面
                    } else {
                        layer.msg(response.msg)
                    }
                }
            })
        });
    });



</script>
</html>